﻿using MediatR;

using Tszy.CodeBuilder.Features;
using Tszy.Unlimited.Base.Extensions;

namespace Tszy.CodeBuilder;

internal class MainExecutor
{
    private readonly IMediator _mediator;
    private readonly CodeExecutor _codeExecutor;

    public MainExecutor(IMediator mediator, CodeExecutor codeExecutor)
    {
        _mediator = mediator;
        _codeExecutor = codeExecutor;
    }

    public void Execute(IServiceProvider serviceProvider, string command)
    {
        OnExecuteAsync(command).Wait();
    }

    private async Task OnExecuteAsync(string command)
    {
        await ExecuteBuildDataModel(command);
    }

    private async Task ExecuteBuildDataModel(string command)
    {
        if (!command.StartsWith("build_data_model"))
            return;

        var parameters = command.Split(' ');

        if (parameters.Length != 3)
        {
            Console.WriteLine("指令参数的个数不对！");
            return;
        }

        var databaseName = parameters[1].TextTrim();
        var tableName = parameters[2].TextTrim();

        if (string.IsNullOrWhiteSpace(databaseName))
        {
            Console.WriteLine("指令参数一不可以是空！");
            return;
        }

        if (string.IsNullOrWhiteSpace(tableName))
        {
            Console.WriteLine("指令参数二不可以是空！");
            return;
        }

        await _codeExecutor.Execute(databaseName, tableName);
    }
}